<?php
// content or excerpt - this is the question
function dt_content_question( $content ) {
    if( false !== strpos($content, '<!--more-->')) {
        return true;
    }
    return false;
}

// edit link
function dt_edit_link( $text = 'Edit', $post_id = null ) {
    if( current_user_can('edit_posts')) {
        
        if( empty($post_id) ) {
            global $post;
            $post_id = $post->ID;
        }
        
    ?>
    <div class="pages">
        <a href="<?php echo get_edit_post_link($post_id); ?>">
            <span><?php _e( $text, LANGUAGE_ZONE ); ?></span>
        </a>
    </div>
    <?php
    
    }
}

// function store come data
function dt_get_where_filter_params( $params = '' ) {
    static $data = '';
    if( !empty($params) ) {
        $data = $params;
        return;
    }
    return $data;
}

// retrieve size match
// second archive item in result - resize flag
function dt_get_size_match( $w = 0, $h = 0, $r_w = null, $r_h = null ) {
    
    if( 1 || (0 == $w && 0 == $h) ) {
        return array( 'large', false );
    }
    
    $sizes = array(
        'thumbnail',
        'medium',
        'large'
    );
    
    // we got specifick height and width
    if( isset($r_w) && isset($r_h) ) {
        $sizes = array( 'custom' );
    }
    
    foreach( $sizes as $size ) {
        if( !(isset($r_w) && isset($r_h)) ) {
            $size_w = get_option($size. '_size_w');
            $size_h = get_option($size. '_size_h');
        }else {
            $size_w = $r_w;
            $size_h = $r_h;
        }
      
        if( 0 == $h ) {
            if( $size_w == $w ) {
                return array( $size, true );
            }elseif( $w < $size_w ) {
                return array( $size, false );
            }
        }elseif( 0 == $w ) {
            if( $size_h == $h ) {
                return array( $size, true );
            }elseif( $h < $size_h ) {
                return array( $size, false );
            }
        }elseif( $size_h == $h && $size_w == $w ) {
            return array( $size, true );
        }elseif( $size_h > $h && $size_w > $w ) {
            return array( $size, false );
        }
    }
    return array( 'full', false );;
}
    
// featured_image_caption function!!!
function the_post_thumbnail_caption( $img_id = null ) {
    if( $img_id ){
        $query = new Wp_Query(array('p' => $img_id, 'post_type' => 'attachment'));
        $thumbnail_image = $query->posts;
        if ($thumbnail_image && isset($thumbnail_image[0])) {
            return $thumbnail_image[0]->post_excerpt;
        }
    }else
        return '';
}
	
//	return image href
function dt_image_href( array $params ){
    // get img id
    if( isset($params['image_id']) ) {
        $img_id = $params['image_id'];
    }elseif( isset($params['post_id']) ) {
        $img_id = get_post_thumbnail_id( $params['post_id'] );
    }else
        return false;
        
    $o_width = isset( $params['width'] )?intval($params['width']):0;
    $o_height = isset( $params['height'] )?intval($params['height']):0;
    
    // get img src
    $img_meta = wp_get_attachment_image_src($img_id, 'large');
        
    // format image url
    if( $img_meta ){
        $img_src = str_replace( site_url(), '', $img_meta[0] );
    }else{
        $img_src = str_replace( site_url(), '', get_template_directory_uri(). '/images/noimage.jpg' ); // noimage image
    }
    
    if( isset($params['upscale']) && false == $params['upscale']){
        // get img metadata( height, width )
        if( $o_width > $img_meta[1] ){
            $o_width = $img_meta[1];
        }
    }
    
    $args = '&zc=1&q=100';//isset($params['no_limits'])?'&no_limits=true':'';
    
    $url_base = get_template_directory_uri(). '/timthumb.php?src=';
    $w = $o_width?'&w='.$o_width:'';
    $h = $o_height?'&h='.$o_height:'';

    $output = $url_base . $img_src . $w . $h. $args;
    
    if ( !isset($params['full_list']) || !$params['full_list'] ) {
        return esc_url($output);
    }else {
        return array( 
            'href'	=>esc_attr($output),
            'size'	=>array(
                'width'		=>$img_meta[1],
                'height'	=>$img_meta[2]
            )
        );
    }
}
	
// get thumbnail
// input array()
// return array()
function dt_get_thumbnail( array $o ) {
    $img_id = false;
    $result = $img_size = array();
    $width = isset($o['width'])?intval($o['width']):0;
    $height = isset($o['height'])?intval($o['height']):0;
    $zc_align = isset($o['zc_align'])?$o['zc_align']:'c';
    $quality = isset($o['quality'])?$o['quality']:100;
    
    // get image id
    if( isset($o['post_id']) ){
        $img_id = get_post_thumbnail_id( intval($o['post_id']) );
    }elseif( isset($o['img_id']) ) {
        $img_id = intval( $o['img_id'] );
    }elseif( isset($o['src']) ) {
        $u_dir = wp_upload_dir();
        $img_path = str_replace($u_dir['baseurl'], $u_dir['basedir'], $o['src']);
        
        // if no such file - return false
        if( !@file_exists($img_path) ) {
            return false;
        }
        
        $img_id = true;
        $src_size = @getimagesize($img_path);
        if( is_array($src_size) ) {
            $img_size[] = $o['src'];
            $img_size[] = $src_size[0];
            $img_size[] = $src_size[1];
            $match_size = dt_get_size_match(
                $width,
                $height,
                $src_size[0],
                $src_size[1]
            );
        }else {
            return false;
        }
        unset($src_size);
    }else {
        return false;
    }
    
    // if no img_id - get first image from gallery
    if( empty($img_id) ) {
        
        global $post;
        if( empty($o['post_id']) ) {
            $o['post_id'] = $post->ID;
        }
        
        $args = array(
            'numberposts'       => 1,
            'order'             => 'ASC',
            'post_mime_type'    => 'image',
            'post_parent'       => $o['post_id'],
            'post_status'       => null,
            'post_type'         => 'attachment' //may use orderby
        );

        $attachments = get_children( $args );
        if( $attachments ) {
            $attachment = current($attachments);
            $img_id = $attachment->ID;
        }
    }
    
    // get image size
    if( empty($img_size) ) {
        $match_size = dt_get_size_match( $width, $height );
        $img_size = wp_get_attachment_image_src($img_id, $match_size[0]);
        
        // if there is no images atall
        if( false == $img_size ) {
            $img_size = array(
                get_template_directory_uri().'/images/noimage.jpg',
                $width,
                $height
            );
            $match_size[1] = false;
        }
    }
    
    // construct result
    if( false === $match_size[1] ) {
        // get post options here
        $args = array(
            'src'   => str_replace(site_url(), '', $img_size[0]),
            'q'     => $quality,
            'a'     => $zc_align
        );
        
        // proportional downscale
        if( !isset($o['upscale']) || false == $o['upscale']) {
            if( $img_size[1] > $width || $img_size[2] > $height ) {
                $proportional_sizes = wp_constrain_dimensions(
                    $img_size[1],
                    $img_size[2],
                    $width,
                    $height
                );
          
                $width = $proportional_sizes[0];
                $height = $proportional_sizes[1];
            }else {
                $width = $img_size[1];
                $height = $img_size[2];
            }
        }else {
        // zoom + crop
            $args['zc'] = 1;
        }
        
        if( $width > 0 ) {
            $args['w'] = $width;
        }else {
            $proportional_sizes = wp_constrain_dimensions(
                $img_size[1],
                $img_size[2],
                $width,
                $height
            );
            $width = $proportional_sizes[0];
        }
        
        if( $height > 0 ) {
            $args['h'] = $height;
        }else {
            $proportional_sizes = wp_constrain_dimensions(
                $img_size[1],
                $img_size[2],
                $width,
                $height
            );
            $height = $proportional_sizes[1];
        }
        
        $result = array(
            'thumnail_img'  => esc_attr(add_query_arg(
                $args,
                get_template_directory_uri().'/timthumb.php'
            )),
            'width'         => $width,
            'height'        => $height
        );
    }else {
        $result = array(
            'thumnail_img'  => esc_attr($img_size[0]),
            'width'         => $img_size[1],
            'height'        => $img_size[2]
        );
    }
    
    $result['size_str'] = "width=\"{$result['width']}\" height=\"{$result['height']}\"";

    return $result;	
}

// function return css options 
function dt_options_css(){
    $fix_pos = of_get_option( 'lv1_fix_bg_checkbox' )?'fixed':'scroll';
?>
    <style type="text/css">
    /* bg 1 */
    .pxs_container {
        background-color: <?php echo of_get_option( 'lv1_back_color_colorpicker' ); ?> !important;
        background-image: <?php echo dt_get_bg('lv1'); ?> !important;
        background-repeat: <?php echo of_get_option( 'lv1_repeat_select' ); ?> !important;
        background-position: <?php echo of_get_option('lv1_horiz_position_select') .' '. of_get_option( 'lv1_vert_position_select' ); ?> !important;
    }
    <?php $fix_pos = of_get_option( 'lv2_fix_bg_checkbox' )?'fixed':'scroll'; ?>
    /* bg 2 */
    .pxs_bg .pxs_bg1 {
        background-image: <?php echo dt_get_bg('lv2'); ?> !important;
        background-repeat: <?php echo of_get_option( 'lv2_repeat_select' ); ?> !important;
        background-position: <?php echo of_get_option('lv2_horiz_position_select') .' '. of_get_option( 'lv2_vert_position_select' ); ?> !important;
    }
    <?php $fix_pos = of_get_option( 'lv3_fix_bg_checkbox' )?'fixed':'scroll'; ?>
    /* bg 3 */ 
    .pxs_bg .pxs_bg2 {
        background-image: <?php echo dt_get_bg('lv3'); ?> !important;
        background-repeat: <?php echo of_get_option( 'lv3_repeat_select' ); ?> !important;
        background-position: <?php echo of_get_option('lv3_horiz_position_select') .' '. of_get_option( 'lv3_vert_position_select' ); ?> !important;
    }
    <?php if( of_get_option( 'fonts_enable_upper_checkbox' ) ): ?>
    /* to upper */
    a.button.big span, #nav > li > a, .box-i-l .box-i-r span.grey, .widget .header, .search-f .p, .go_up, .foll .head {
        text-transform: uppercase !important;
    }
    <?php endif ?>
    /* top area */
    #breadcrumbs_bg {
        background-image: <?php echo dt_get_bg('top'); ?> !important;
        background-repeat: <?php echo of_get_option( 'top_repeat_select' ); ?> !important;
        background-position: <?php echo of_get_option('top_horiz_position_select') .' '. of_get_option( 'top_vert_position_select' ); ?> !important;
        background-color: <?php echo of_get_option( 'top_back_color_colorpicker' ); ?> !important;
    }
    /* body */
    <?php $fix_pos = of_get_option( 'body_fix_bg_checkbox' )?'fixed':'scroll'; ?>
    body {
        background-image: <?php echo dt_get_bg('body'); ?> !important;
        background-repeat: <?php echo of_get_option( 'body_repeat_select' ); ?> !important;
        background-position: <?php echo of_get_option('body_horiz_position_select') .' '. of_get_option( 'body_vert_position_select' ); ?> !important;
        background-color: <?php echo of_get_option( 'body_back_color_colorpicker' ); ?> !important;
        background-attachment: <?php echo $fix_pos; ?> !important;
    }
    </style>
<?php
}

// return background image: $level = f/s/t
function dt_get_bg( $level='lvl' ){
    if( of_get_option( $level. '_custom_bg_checkbox' ) ){
        $img = of_get_option( $level. '_custom_db_uploader' );
        if((strpos( $img, '.png') || strpos( $img, '.jpg') || strpos( $img, '.gif')) ){
            $output = $img;
        }
    }else{
        $output = of_get_option( $level. '_back_images' );
    }
    
    if( empty($output) || ('none' == $output)){
        return 'none';
    }
    return $output = 'url("' .$output. '")';
}

// widgitizeb footer selector
function dt_show_wfooter() {
    $choise = of_get_option( 'wf_select', 'every' );
    switch( $choise ) {
        case 'home':
            if( is_front_page() ) {
                return true; 
            }
            break;
        case 'ex_home':
            if( !is_front_page() ) {
                return true; 
            }
            break;
        case 'nowhere':
            return false;
        default:
            return true;
    }
    return false;
}

if( false ) {
    add_editor_style();
    add_custom_image_header();
    add_custom_background();
}

//-----------------------------BREADCRUMBS--------------------------------//

// original script you can find on http://dimox.net
function dimox_breadcrumbs( $args = array() ) {
    $defaults = array(
        'delimiter'         => '&raquo;',
        'home'              => 'Home',
        'before'            => '<span class="current">',
        'after'             => '</span>',
        'before_wrapper'    => '<div id="crumbs">',
        'after_wrapper'     => '</div>',
        'before_link'       => '<li>',
        'after_link'        => '</li>'
    );
    
    $opts = wp_parse_args( $args, $defaults );
    
    $delimiter = $opts['delimiter'];
    $home = $opts['home']; // text for the 'Home' link
    $before = $opts['before']; // tag before the current crumb
    $after = $opts['after']; // tag after the current crumb
    $b_link = $opts['before_link'];
    $a_link = $opts['after_link'];
    $title = '';
    $sub_title = '';
    
    if ( 1 || (!is_home() && !is_front_page()) || is_paged() ) {

        echo $opts['before_wrapper'];
 
        $out = '<ul>';
        
        global $post;
        $homeLink = get_bloginfo('url');
        $out .= $b_link . '<a href="' . $homeLink . '">' . $home . '</a>' . $a_link . $delimiter;
 
        if ( is_category() ) {
            global $wp_query;
            $cat_obj = $wp_query->get_queried_object();
            $thisCat = $cat_obj->term_id;
            $thisCat = get_category($thisCat);
            $parentCat = get_category($thisCat->parent);
            if ($thisCat->parent != 0) {
                $out .= $b_link . (get_category_parents($parentCat, TRUE, $delimiter)) . $a_link;        
            }
        
            $title = __( 'Archive by category "%"', LANGUAGE_ZONE );
            $sub_title = single_cat_title('', false);
            $title = str_replace( '%', $sub_title, $title );
            
        }elseif ( is_day() ) {
            $out .= $b_link . '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $a_link . $delimiter;
            $out .= $b_link . '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a>' . $a_link . $delimiter;
            $title = $sub_title = get_the_time('d');
 
        }elseif ( is_month() ) {
            $out .= $b_link . '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $a_link . $delimiter;
            $title = $sub_title = get_the_time('F');
 
        }elseif ( is_year() ) {
            $title = $sub_title = get_the_time('Y');
 
        }elseif ( is_search() ) {
            $title = __( 'Search results for "%"', LANGUAGE_ZONE );
            $sub_title = get_search_query();
            $title = str_replace( '%', $sub_title, $title );
 
        }elseif ( is_single() && !is_attachment() ) {
            if ( get_post_type() != 'post' ) {
                $post_type = get_post_type_object(get_post_type());
                $slug = $post_type->rewrite;
 //               echo $b_link . $post_type->labels->singular_name . $a_link . $delimiter;
//                echo $b_link . '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>' . $a_link . $delimiter;
                $title = $sub_title = get_the_title();
            }else {
// blog and other stuff
                $menu_data = dt_current_menu_item();
                
//                $cat = get_the_category(); $cat = $cat[0];
//                echo $b_link . get_category_parents($cat, TRUE, ' ' . $delimiter . ' ') . $a_link;
                $out .= $b_link . '<a href=' . esc_url($menu_data['link']) . '>' . $menu_data['title'] . '</a>' . $a_link;
                $title = get_the_title();
                $sub_title = $menu_data['title'];
            }
 
        }elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
            $post_type = get_post_type_object(get_post_type());
            $title = $sub_title = $post_type->labels->singular_name;
 
        }elseif ( is_attachment() ) {
            $parent = get_post($post->post_parent);
//            $cat = get_the_category($parent->ID); $cat = $cat[0];
//            $out .= get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
            $out .= $b_link . '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>'  . $a_link . $delimiter;
            $title = $sub_title = get_the_title();
 
        }elseif ( is_page() && !$post->post_parent ) {
            $title = $sub_title = get_the_title();
 
        }elseif ( is_page() && $post->post_parent ) {
            $parent_id  = $post->post_parent;
            $breadcrumbs = array();
            while ($parent_id) {
                $page = get_page($parent_id);
                $breadcrumbs[] = $b_link . '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>' . $a_link;
                $parent_id  = $page->post_parent;
            }
            $breadcrumbs = array_reverse($breadcrumbs);
            foreach ($breadcrumbs as $crumb) $out .= $crumb . $delimiter;
            $title = $sub_title = get_the_title();
 
        }elseif ( is_tag() ) {
            $title = __( 'Posts tagged "%"', LANGUAGE_ZONE );
            $sub_title = single_tag_title('', false);
            $title = str_replace( '%', $sub_title, $title );
 
        }elseif ( is_author() ) {
            global $author;
            $userdata = get_userdata($author);
            $title = __( 'Articles posted by %', LANGUAGE_ZONE );
            $sub_title = $userdata->display_name;
            $title = str_replace( '%', $sub_title, $title );
        
        }elseif ( is_404() ) {
            $title = $sub_title = __( 'Error 404', LANGUAGE_ZONE );
        
        }elseif( is_home() ) {
            $title = $sub_title = __( 'Blog', LANGUAGE_ZONE );
            
        }
        
        if( strlen($title) > 50 ) {
            $title = substr( $title, 0, 50 );
            $title .= '...';
        }
        
        $out .= $before . $title . $after;

        if ( get_query_var('paged') ) {
            if( is_category() ||
                is_day() ||
                is_month() ||
                is_year() ||
                is_search() ||
                is_tag() ||
                is_author() )
            {
                $out .= ' (';
            }
            
            $out .= '&nbsp;' . __('Page', LANGUAGE_ZONE) . ' ' . get_query_var('paged');
            
            if( is_category() ||
                is_day() ||
                is_month() ||
                is_year() ||
                is_search() ||
                is_tag() ||
                is_author() )
            {
                $out .= ')';
            }
        }
        
        $out .= '</ul>';
        
        if( $opts['show_trail'] ) {
            echo $out;
        }
        
        echo '<h1>' . dt_first_letter_filter( $sub_title ) . '</h1>';
        
        echo $opts['after_wrapper'];
 
    }
} // end dimox_breadcrumbs